package cc.fs.kotlinproject.utlis

import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app
import android.support.v7.app.AlertDialog
import cc.fs.kotlinproject.R

/**
 * Created by fostion on 2015/8/14.
 */
class AlertDialogHelper2 : DialogFragment() {

    companion object {
        var builder: AlertDialog.Builder ? = null

        public fun newInstance(context: Context): AlertDialogHelper2 {
            builder = AlertDialog.Builder(context)
            return AlertDialogHelper2()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog? {
        e("------recreate 22222")
        return builder?.create();
    }

    public fun title(title: Any): AlertDialogHelper2? {
        builder?.title(title)
        return this
    }

    public fun content(content: Any): AlertDialogHelper2? {
        builder?.content(content)
        return this
    }

    public fun canCancelable(canceable: Boolean): AlertDialogHelper2? {
        builder?.setCancelable(canceable)
        return this
    }

    public fun sure(sure: Any, callable: (DialogInterface, Int) -> Unit?): AlertDialogHelper2? {
        builder?.sure(sure, object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface, which: Int) {
                callable?.invoke(dialog, which)
            }
        })
        return this
    }

    public fun cancle(sure: Any, callable: (DialogInterface, Int) -> Unit?): AlertDialogHelper2? {
        builder?.cancle(sure, object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface, which: Int) {
                callable?.invoke(dialog, which)
            }
        })
        return this
    }

    public fun show(): AlertDialogHelper2? {
        builder?.show()
        return this
    }


    fun AlertDialog.Builder.title(title: Any): AlertDialog.Builder {
        when (title) {
            is Int -> return setTitle(title as Int)
            else -> return setTitle(title as String)
        }
    }

    fun AlertDialog.Builder.content(content: Any): AlertDialog.Builder {
        when (content) {
            is Int -> return setMessage(content as Int)
            else -> return setMessage(content as String)
        }
    }

    fun AlertDialog.Builder.sure(content: Any, onClick: DialogInterface.OnClickListener): AlertDialog.Builder {
        when (content) {
            is Int -> return setPositiveButton(content as Int, onClick)
            else -> return setPositiveButton(content as String, onClick)
        }
    }

    fun AlertDialog.Builder.cancle(content: Any, onClick: DialogInterface.OnClickListener): AlertDialog.Builder {
        when (content) {
            is Int -> return setNegativeButton(content as Int, onClick)
            else -> return setNegativeButton(content as String, onClick)
        }
    }
}


